package com.itheima.controller.pms;

import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.pms.PmsProductCategory;
import com.itheima.pojo.pms.vo.PmsProductCategoryVo;
import com.itheima.pojo.pms.vo.PmsProductCategoryWithChildrenItemVo;
import com.itheima.service.pms.PmsProductCategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类管理Controller
 */
@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {
    @DubboReference
    private PmsProductCategoryService pmsProductCategoryService;

    /**
     * 添加商品分类
     * @param pmsProductCategoryVo
     * @return
     */
    @PostMapping("create")
    public R create(@Validated @RequestBody PmsProductCategoryVo pmsProductCategoryVo) {
        PmsProductCategory ppc = new PmsProductCategory();
        BeanUtils.copyProperties(pmsProductCategoryVo,ppc);
        return R.success(pmsProductCategoryService.save(ppc));
    }

    /**
     *  删除商品分类
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        return R.success(pmsProductCategoryService.removeById(id));
    }

    /**
     * 查询所有一级分类及子分类
     * @return
     */
    @GetMapping("/list/withChildren")
    public R listWithChildren() {
        List<PmsProductCategory> list = pmsProductCategoryService.listWithChildren();
        return R.success(list);
    }

    /**
     * 修改显示状态
     * @param ids
     * @param showStatus
     * @return
     */
    @PostMapping("/update/showStatus")
    public R updateShowStatus(@RequestParam("ids") List<Long> ids,@RequestParam("showStatus") int showStatus){
        pmsProductCategoryService.updateShowStatus(ids,showStatus);
        return R.success("修改显示状态成功！");
    }


    /**
     * 修改导航栏显示状态
     * @param ids
     * @param navStatus
     */
    @PostMapping("/update/navStatus")
    public R updateNavStatus(@RequestParam("ids") List<Long> ids, @RequestParam("navStatus") int navStatus) {
        pmsProductCategoryService.updateNavStatus(ids, navStatus);
        return R.success("修改导航栏显示状态成功！");

    }

    /**
     * 修改商品分类
     * @param id
     * @param pmsProductCategoryVo
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateProductCategory(@PathVariable Long id,@RequestBody PmsProductCategoryVo pmsProductCategoryVo){
        pmsProductCategoryService.updateProductCategory(id,pmsProductCategoryVo);
        return R.success("修改商品分类成功！");
    }

    /**
     * 根据id获取商品分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getCategory(@PathVariable Long id){
        PmsProductCategory productCategory = pmsProductCategoryService.getById(id);
        return R.success(productCategory);
    }

    /**
     * 分页查询商品分类
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{parentId}")
    public R list(@PathVariable Long parentId,Integer pageNum,Integer pageSize){
       PageInfo pageInfo= pmsProductCategoryService.listByPage(parentId,pageNum,pageSize);
       return R.success(pageInfo);
    }

}
